define(function(require) {
	// var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function() {
		this.callParent();
	};

	Model.prototype.ReturnBtnClick = function(event) {
		this.getParent().comp("Main_Contents").to("ScanningDelivery_Content");
	};

	Model.prototype.NextBtnClick = function(event) {
		this.SellInsert();
	};

	Model.prototype.checkInput = function() {
		if (this.comp("name_input").val() === undefined || this.comp("name_input").val().length < 1) {
			justep.Util.hint("请输入姓名");
			return false;
		}
		if (this.comp("phone_input").val() === undefined || this.comp("phone_input").val().length < 1) {
			justep.Util.hint("请输入手机号码");
			return false;
		}
		if (this.comp("Locate_data").val("col0") === undefined || this.comp("Locate_data").val("col0").length < 1) {
			justep.Util.hint("请选择省份");
			return false;
		}
		if (this.comp("Locate_data").val("col1") === undefined || this.comp("Locate_data").val("col1").length < 1) {
			justep.Util.hint("请选择城市");
			return false;
		}
		if (this.comp("Locate_data").val("col2") === undefined || this.comp("Locate_data").val("col2").length < 1) {
			justep.Util.hint("请选择区县");
			return false;
		}
		if (this.comp("address_input").val() === undefined || this.comp("address_input").val().length < 1) {
			justep.Util.hint("请输入详细地址");
			return false;
		}
		return true;

	};
	Model.prototype.SellInsert = function() {
		console.log("SellInsert()");
		var api_url = window.app.base_url + window.app.SellInsertAPI;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;

		if (this.checkInput() === false) {
			return;
		}
		data.CtmNo = window.app.UserApi_ret.CtmNo;
		data.SellName = this.comp("name_input").val();
		data.Phone = this.comp("phone_input").val();
		data.Address = this.comp("address_input").val();
		data.Province = this.comp("Locate_data").val("col0");
		data.City = this.comp("Locate_data").val("col1");
		data.County = this.comp("Locate_data").val("col2");
		data.DeliveryDate = justep.Date.toString(new Date(), "yyyyMMdd");
		data.Apply = window.app.UserApi_ret.CtmName;
		data.CtmType = window.app.UserApi_ret.CtmType;
		
		console.log(data);
		var self = this;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				window.app.SellInsert_ret = res;
				var params = {};
				params.SID = res[0].ID;
				params.SN = res[0].SN;
				params.PrdArray = [];
				self.getParent().comp("AddProductsSell_Window").load("$UI/ls_shaomiao/AddProductsSell.w", params);
				self.getParent().comp("Main_Contents").to("AddProductsSell_Content");
				return;
			},
			error : function(e) {
				return;
			}
		});
	};
	return Model;
});